/******************************************************************************* Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This file contains the "main" function for a project. Description: This file contains the "main" function for a project. The "main" function calls the "SYS_Initialize" function to initialize the state machines of all modules in the system *******************************************************************************/ // ***************************************************************************** // ***************************************************************************** // Section: Included Files // ***************************************************************************** // ***************************************************************************** #include <stddef.h> // Defines NULL #include <stdbool.h> // Defines true #include <stdlib.h> // Defines EXIT_FAILURE #include "definitions.h" // SYS function prototypes #include <stdio.h> #include "lcd_ACM1602_lib_i2c.h" int delay_Clock = 200000000; //200MHz char Buf[32]; void delay_us(volatile unsigned int usec) //1μsec遅延 { volatile int count; count = (int)(delay_Clock/20000000)*usec; do //実測 at 200MH (Clock=200000000) { //delay_us(1000):1000.4μsec delay_us(100):100.6μsec delay_us(10):10.5μsec delay_us(1):1.5μsec asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); count--; }while(count != 0); } void delay_ms(volatile unsigned int msec) //1msec遅延 { volatile unsigned int i; //実測:at200MH (Clock=200000000)//delay_ms(1): 1.0006msec delay_ms(100):100.04msec for(i=0; i<msec; i++) delay_us(1000); } void AdcFunc(void) //AD変換、表示 { unsigned int AdcValue; float Volt; //AD変換開始 Trigger a conversion ADCCON3bits.GSWTRG = 1; //AD変換ソフトウェアトリガ //Global Level Software Trigger bit //変換完了を待つ Wait the conversions to complete while (ADCDSTAT1bits.ARDY7 == 0); //結果の取り出し fetch the result AdcValue = ADCDATA7; Volt =(float)AdcValue/4095*3.3; //ボルト表示化 lcd_ACM1602_cmd_i2c(0x80); //1行目の先頭へ sprintf(Buf,"AdV=%u ",AdcValue); //バッファーに文字列をセット lcd_ACM1602_str_i2c(Buf); lcd_ACM1602_cmd_i2c(0xC0); //2行目の先頭へ sprintf(Buf,"Volt=%.2f[V] ",Volt); //文字列としてバッファーに収納 lcd_ACM1602_str_i2c(Buf); delay_ms(250); } void APP_Initialize ( void ) { // Configure ADCCON1 ADCCON1 = 0; // ADCCON1bits.FRACT = 0; // use Integer output format ADCCON1bits.SELRES = 3; // ADC7 resolution is 12 bits //default ADCCON1bits.STRGSRC = 0; // No scan trigger. // Configure ADCCON2 ADCCON2 = 0; ADCCON2bits.SAMC = 5; // ADC7 sampling time = 5 * TAD7 ADCCON2bits.ADCDIV = 1; // ADC7 clock freq is half of control clock = TAD7 // Configure ADCCON3 ADCCON3 = 0; ADCCON3bits.ADCSEL = 0; //クロックソース:PBCLK3 // Select input clock source ADCCON3bits.CONCLKDIV = 1; // Control clock frequency is half of input clock ADCCON3bits.VREFSEL = 0; // Select AVdd and AVss as reference source ADCCON3bits.DIGEN7 = 1; //ADC7:有効 //Shared ADC (ADC7) Digital Enable bit ADCCON3bits.RQCNVRT = 1; //個別ポート入力モード //Individual ADC Input Conversion Request bit ADCCON3bits.ADINSEL = 8; //入力ポートに AN8 設定 //Analog Input Select bits ADCCON3bits.SAMP = 1; //クラス2モード設定 //Class 2 and Class 3 Analog Input Sampling Enable bit // No selection for dedicated ADC modules, no pre sync trigger, not sync sampling ADCTRGMODE = 0; // Select ADC input mode //ADC INPUT MODE CONTROL REGISTER 1 ADCIMCON1bits.SIGN8 = 0; // unsigned data format ADCIMCON1bits.DIFF8 = 0; // Single ended mode ADCTRG2bits.TRGSRC7 = 1; //AN7(ADC7)をソフトウェアトリガに設定 //Trigger Source for Conversion of Analog Input AN7 Select bits //必須 // Turn the ADC on ADCCON1bits.ON = 1; // Enable clock to analog circuit ADCANCONbits.ANEN7 = 1; // Enable the clock to analog bias // Wait for ADC to be ready while(!ADCANCONbits.WKRDY7); // Wait until ADC7 is ready // Enable the ADC module ADCCON3bits.DIGEN7 = 1; // ADC7 ADCデジタル回路ON lcd_ACM1602_init_i2c(); //I2Cインターフェース式液晶初期化 lcd_ACM1602_cmd_i2c(0x0C); //カーソル:0FF、ブリンク:0FF lcd_ACM1602_cmd_i2c(0x80); //1行目の先頭へ lcd_ACM1602_str_i2c("CN2: AN8_ADC7 "); lcd_ACM1602_cmd_i2c(0xC0); //2行目の先頭へ lcd_ACM1602_str_i2c(" I2C5_LCD "); delay_ms(2000); } // ***************************************************************************** // ***************************************************************************** // Section: Main Entry Point // ***************************************************************************** // ***************************************************************************** int main ( void ) { /* Initialize all modules */ SYS_Initialize ( NULL ); APP_Initialize ( ); while ( true ) { /* Maintain state machines of all polled MPLAB Harmony modules. */ SYS_Tasks ( ); AdcFunc(); } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); } /******************************************************************************* End of File */